package com.wnxy.sdd.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnxy.sdd.common.exception.BusinessException;
import com.wnxy.sdd.common.result.Result;
import com.wnxy.sdd.common.result.enums.ResultEnum;
import com.wnxy.sdd.entity.Course;
import com.wnxy.sdd.model.vo.CoursePublishVo;
import com.wnxy.sdd.model.vo.CourseVo;
import com.wnxy.sdd.model.vo.PageVo;
import com.wnxy.sdd.service.ICourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Jet
 * @since 2023-06-19
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    private final ICourseService courseService;
    // 构造器注入：根据参数类型；如果类型有多个，根据参数名称去ioc容器找该名称对应的对象注入；
    // 如果名称不存在，报错。
    public CourseController(ICourseService courseService) {
        this.courseService = courseService;
    }

    /**
     * 添加课程
     */
    @PostMapping
    @ApiOperation("添加课程")
    public Result saveOrUpdateCourse(@RequestBody CourseVo courseVo){
        // 根据前端页面跳转要求，保存后需要返回课程id
        String courseId = courseService.saveOrUpdateCourse(courseVo);
        return Result.ok(courseId);
    }

    /**
     * 根据id查询
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询")
    public Result findCourseById(@PathVariable("id")String id){
        if (id == null || "".equals(id) || Integer.parseInt(id) < 0) {
            throw new BusinessException(ResultEnum.INVALID_PARAMS);
        }

        CourseVo courseVo = courseService.findCourseById(id);
        return Result.ok(courseVo);
    }

    /**
     * 根据id查询课程信息、培训师、课程类别
     */
    @GetMapping("/publish/{courseId}")
    @ApiOperation("查询课程信息、培训师、课程类别")
    public Result findCoursePublish(@PathVariable("courseId")String courseId){
        CoursePublishVo coursePublishVo = courseService.findCoursePublish(courseId);
        return Result.ok(coursePublishVo);
    }

    /**
     * 修改课程状态
     */
    @PutMapping("/update/{courseId}")
    @ApiOperation("修改课程状态")
    public Result updateStatus(@PathVariable("courseId")String courseId){
        //UPDATE sdd_course SET STATUS='Normal' WHERE id=''
        LambdaUpdateWrapper<Course> wrapper = Wrappers.lambdaUpdate(Course.class);
        wrapper.set(Course::getStatus,"Normal");
        wrapper.eq(Course::getId,courseId);
        courseService.update(wrapper);
        return Result.ok();
    }

    /**
     * 分页查询
     */
    @GetMapping("/page/{current}/{size}")
    @ApiOperation("分页查询")
    public Result findByPage(
            @PathVariable("current") Integer current,
            @PathVariable("size") Integer size){
        // 构造分页参数
        IPage<Course> pageParam = new Page<>(current,size);
        // 分页查询
        IPage<Course> pageData = courseService.page(pageParam);
        // 返回PageVo
        PageVo<Course> pageVo = new PageVo<>();
        pageVo.setList(pageData.getRecords());
        pageVo.setTotal(pageData.getTotal());
        return Result.ok(pageVo);
    }
}
